import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

/**
 * Mar 28, 2021  copy right
 */

/**
 * @author code260@qq.com
 *
 */
public class ArthasSide {
	
	private static ConcurrentMap<String, Boolean> name2Flag = new ConcurrentHashMap<String, Boolean>();
	
	/**
	 * 设置挂起名称  并可以指定挂起多少毫秒， 可以用在watch的表达式上
	 * @param name
	 * @param millsecond
	 * @return
	 */
	public static boolean hang(String name, long millsecond) {
		name2Flag.put(name, true);
		long start = System.currentTimeMillis();
		while (true) {
			long now = System.currentTimeMillis();
			if (now - start > millsecond) {
				break;
			}
			Boolean hangFlag = name2Flag.get(name);
			if (hangFlag != null && !hangFlag) {
				break;
			}
			try {
				TimeUnit.MILLISECONDS.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
	
	/**
	 * 解除挂起  可以用arthas的 ongl指令  比如  ognl '@ArthasSide@breakHang("xxname")'
	 * @param name
	 * @return
	 */
	public static boolean breakHang(String name) {
		name2Flag.remove(name);
		return true;
	}

}
